# -*- coding: utf-8 -*-
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
import smtplib

mail = {
    "mail_host": "smtp.exmail.qq.com",  # 设置服务器
    "mail_user": "yangyong@astoutiao.com",  # 用户名
    "mail_pass": "aA123456",  # 口令
    "sender": "yangyong@astoutiao.com",  # 邮件发送人
    # 接收人列表
    "receivers": ['yangyong@astoutiao.com']
}

class FailMail(object):
    @staticmethod
    def __instant_email(subject, text, *args, **kwargs):
        msgRoot = MIMEMultipart('related')
        msgRoot['From'] = mail["sender"]
        msgRoot['To'] = ",".join(mail["receivers"])
        msgRoot['Subject'] = Header(subject, 'utf-8')
        # 添加附件
        msgAlternative = MIMEMultipart('alternative')
        msgRoot.attach(msgAlternative)
        msgAlternative.attach(MIMEText(text, 'html', 'utf-8'))
        smtpObj = smtplib.SMTP()
        smtpObj.connect(mail["mail_host"], 25)  # 25 为 SMTP 端口号
        smtpObj.login(mail["mail_user"], mail["mail_pass"])
        smtpObj.sendmail(mail["sender"], mail["receivers"], msgRoot.as_string())

    @staticmethod
    def send_mail(subject, text, *args, **kwargs):
        try:
            FailMail.__instant_email(subject, text, *args, **kwargs)
        except Exception as e:
            print("邮件发送失败: ", e)
        else:
            print("邮件发送成功")

if __name__ == '__main__':
    pass